package com.dhcc.DRGsHosp.facade.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.openkoala.koalacommons.resourceloader.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DRGsApplication;
import com.dhcc.DRGsHosp.application.DepGroupApplication;
import com.dhcc.DRGsHosp.application.DepartmentApplication;
import com.dhcc.DRGsHosp.application.ExpectedApplication;
import com.dhcc.DRGsHosp.application.ForeCastApplication;
import com.dhcc.DRGsHosp.application.QuotaExpectedApplication;
import com.dhcc.DRGsHosp.core.domain.DepGroup;
import com.dhcc.DRGsHosp.core.domain.Department;
import com.dhcc.DRGsHosp.core.domain.Expected;
import com.dhcc.DRGsHosp.core.domain.QuotaExpected;
import com.dhcc.DRGsHosp.facade.ForeCastFacade2;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;

/**
 * @title		:ForeCastFacade2Impl.java
 * @description	:
 * @author		:lileizheng
 * @date		:2015年7月18日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named("drgsHospFacadeImpl19")
public class ForeCastFacade2Impl implements ForeCastFacade2 {

	@Autowired
	private ExpectedApplication expectedApplication;
	
	@Autowired
	private QuotaExpectedApplication quotaExpectApplication;
	
	@Autowired
	private DepGroupApplication depGroupApplication;
	
	@Autowired 
	private DepartmentApplication departmentApplication;

	/**
	 * 
	 *  @Description：保存测算结果
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.facade.ForeCastFacade2#saveExpected(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
	 */
	@Override
	public InvokeResult saveExpected(IndexCalDTO dto) {/*
		String year = ForeCastFacadeImpl.year;
		String expectedStr = dto.getType2();
		String expectedType = dto.getType1();
		String[] expected = expectedStr.split("\\|");
		String ccode = expected[0];
		Expected e = new Expected();
		String id = "";
		if("hosp".equals(ccode)){
			id = "hosp"+year;
		}else{
			if(StringUtils.hasLength(dto.getPardepgroupCode())){
				System.err.println(dto.getPardepgroupCode());
				id = dto.getPardepgroupCode()+year;
				e.setParDepGroupCode(dto.getPardepgroupCode());
			}else if(StringUtils.hasLength(dto.getPardepCode())){
				System.err.println(dto.getPardepCode());
			//	id = dto.getPardepCode()+year;
			//	e.setParDepCode(dto.getPardepCode());
			}else if(StringUtils.hasLength(dto.getParmdcCode())){
				System.err.println(dto.getParmdcCode());
			//	id = dto.getParmdcCode()+year;
			//	e.setParMDCCode(dto.getParmdcCode());
			}else if(StringUtils.hasLength(dto.getParadrgCode())){
				System.err.println(dto.getParadrgCode());
				id = dto.getParadrgCode()+year;
			//	e.setParADRGCode(dto.getParadrgCode());
			}else{
			//	id = dto.getPardrgCode()+year;
			//	e.setPardrgCode(dto.getPardrgCode());
			}
		}
		e.setExpectedCode(id+expectedType);
		//e.setExpectedType(expectedType);
	//	expectedApplication.updateExpected(e);
		String expectQuota = expected[1];
		String[] expectQuotas = expectQuota.split(",");
		for(String quota: expectQuotas){
			String[] quotas = quota.split(":");
			String quotaCode = quotas[0];
			String quotaValue = quotas[1];
			QuotaExpected qe = new QuotaExpected();
			qe.setParExpectedCode(id+expectedType);
			String idd = id + "-" +quotaCode;
			qe.setId(idd);
			qe.setParQuotaCode(quotaCode);
			qe.setExpectedValue(quotaValue!=null?Double.parseDouble(quotaValue):0);
			quotaExpectApplication.updateQuotaExpected(qe);
		}
		
		return InvokeResult.success();
	*/
		return InvokeResult.success();
	}
	
	/**
	 * @methodname    :getLastResultHosp
	 * @description   : 获取全院上次测算数据
	 * @param         :
	 * @author        :lileizheng
	 * @return        :
	 * @date          :2015年7月20日
	 */
	public InvokeResult getLastResultHosp(IndexCalDTO dto){
		Map<String, Object> resultMap = new HashMap<>();
		List<Expected> hospExpected = expectedApplication.getExpected(dto.getType1(),ForeCastFacadeImpl.year, "hosp");
		if(hospExpected.size()>0){
			Expected e = hospExpected.get(0);
			String eCode = e.getExpectedCode();
			List<QuotaExpected> dtlList =null; //quotaExpectApplication.getQuotaExpectedByCode(eCode);
			for(QuotaExpected quotaExpected: dtlList){
				resultMap.put(quotaExpected.getParQuotaCode(), quotaExpected.getExpectedValue());
//				if("AvgDays".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("AvgDays", quotaExpected.getExpectedValue());
//				}
//				if("AvgFee".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("AvgFee", quotaExpected.getExpectedValue());
//				}
//				if("HCAvgFee".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("HCAvgFee", quotaExpected.getExpectedValue());
//				}
//				if("DrugFee".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("DrugFee", quotaExpected.getExpectedValue());
//				}
//				if("HCDrugFee".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("HCDrugFee", quotaExpected.getExpectedValue());
//				}
//				if("TotalCaseCnt".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("TotalCaseCnt", quotaExpected.getExpectedValue());
//				}
//				if("HCCaseCnt".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("HCCaseCnt", quotaExpected.getExpectedValue());
//				}
//				if("HCTotalFund".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("HCTotalFund", quotaExpected.getExpectedValue());
//				}
//				if("TimeEff".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("TimeEff", quotaExpected.getExpectedValue());
//				}
//				if("FeeEff".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("FeeEff", quotaExpected.getExpectedValue());
//				}
//				if("DrugMateFee".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("DrugMateFee", quotaExpected.getExpectedValue());
//				}
//				if("HCDrugMateFee".equals(quotaExpected.getParQuotaCode())){
//					resultMap.put("HCDrugMateFee", quotaExpected.getExpectedValue());
//				}
			}
		}else{
			resultMap = null;
		}
		
		return InvokeResult.success(resultMap);
	}
	
	
	@Override
	public InvokeResult getLastResultDepGroup(IndexCalDTO dto) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<Expected> depGroupExpected = expectedApplication.getExpected(dto.getType1(),ForeCastFacadeImpl.year, "depGroup");
		if(depGroupExpected.size()>0){/*
			for(int i=0; i<depGroupExpected.size(); i++){
				Map<String, Object> resultMap = new HashMap<>();
				Expected e = depGroupExpected.get(i);
				String eCode = e.getExpectedCode();
				resultMap.put("depGroupCode", e.getParDepGroupCode());
				DepGroup depGroup = depGroupApplication.getDepGroup(e.getParDepGroupCode());
				resultMap.put("depGroupDesc", depGroup.getDepGroupDesc());
				List<QuotaExpected> dtlList = quotaExpectApplication.getQuotaExpectedByCode(eCode);
				for(QuotaExpected quotaExpected: dtlList){
					if("AvgDays".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgDays", quotaExpected.getExpectedValue());
					}
					if("AvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgFee", quotaExpected.getExpectedValue());
					}
					if("HCAvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcAvgFee", quotaExpected.getExpectedValue());
					}
					if("DrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugFee", quotaExpected.getExpectedValue());
					}
					if("TotalCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("totalCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCTotalFund".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcTotalFund", quotaExpected.getExpectedValue());
					}
					if("TimeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("timeEff", quotaExpected.getExpectedValue());
					}
					if("FeeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("feeEff", quotaExpected.getExpectedValue());
					}
					if("DrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugMateFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugMateFee", quotaExpected.getExpectedValue());
					}
				}
				resultList.add(resultMap);
			}
		*/}else{
			return null;
		}
		return InvokeResult.success(resultList);
	}
	
	@Override
	public InvokeResult getLastResultDep(IndexCalDTO dto) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<Expected> depExpected = expectedApplication.getExpected(dto.getType1(),ForeCastFacadeImpl.year, "dep");
		if(depExpected.size()>0){/*
			for(int i=0; i<depExpected.size(); i++){
				Map<String, Object> resultMap = new HashMap<>();
				Expected e = depExpected.get(i);
				String eCode = e.getExpectedCode();
				resultMap.put("depCode", e.getParDepCode());
				Department dep = departmentApplication.getDepartment(e.getParDepCode());
				resultMap.put("depDesc", dep.getDepName());
				List<QuotaExpected> dtlList = quotaExpectApplication.getQuotaExpectedByCode(eCode);
				for(QuotaExpected quotaExpected: dtlList){
					if("AvgDays".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgDays", quotaExpected.getExpectedValue());
					}
					if("AvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgFee", quotaExpected.getExpectedValue());
					}
					if("HCAvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcAvgFee", quotaExpected.getExpectedValue());
					}
					if("DrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugFee", quotaExpected.getExpectedValue());
					}
					if("TotalCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("totalCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCTotalFund".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcTotalFund", quotaExpected.getExpectedValue());
					}
					if("TimeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("timeEff", quotaExpected.getExpectedValue());
					}
					if("FeeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("feeEff", quotaExpected.getExpectedValue());
					}
					if("DrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugMateFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugMateFee", quotaExpected.getExpectedValue());
					}
				}
				resultList.add(resultMap);
			}
		*/}else{
			return null;
		}
		return InvokeResult.success(resultList);
	}

	@Override
	public InvokeResult getLastResultMDC(IndexCalDTO dto) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<Expected> expected = expectedApplication.getExpected(dto.getType1(),ForeCastFacadeImpl.year, "mdc");
		if(expected.size()>0){
			for(int i=0; i<expected.size(); i++){/*
				Map<String, Object> resultMap = new HashMap<>();
				Expected e = expected.get(i);
				String eCode = e.getExpectedCode();
			//	resultMap.put("mdcCode", e.getParMDCCode());
				List<QuotaExpected> dtlList = quotaExpectApplication.getQuotaExpectedByCode(eCode);
				for(QuotaExpected quotaExpected: dtlList){
					if("AvgDays".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgDays", quotaExpected.getExpectedValue());
					}
					if("AvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgFee", quotaExpected.getExpectedValue());
					}
					if("HCAvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcAvgFee", quotaExpected.getExpectedValue());
					}
					if("DrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugFee", quotaExpected.getExpectedValue());
					}
					if("TotalCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("totalCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCTotalFund".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcTotalFund", quotaExpected.getExpectedValue());
					}
					if("TimeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("timeEff", quotaExpected.getExpectedValue());
					}
					if("FeeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("feeEff", quotaExpected.getExpectedValue());
					}
					if("DrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugMateFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugMateFee", quotaExpected.getExpectedValue());
					}
				}
				resultList.add(resultMap);
			*/}
		}else{
			return null;
		}
		return InvokeResult.success(resultList);
	}

	@Override
	public InvokeResult getLastResultADRG(IndexCalDTO dto) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<Expected> expected = expectedApplication.getExpected(dto.getType1(),ForeCastFacadeImpl.year, "adrg");
		if(expected.size()>0){
			for(int i=0; i<expected.size(); i++){/*
				Map<String, Object> resultMap = new HashMap<>();
				Expected e = expected.get(i);
				String eCode = e.getExpectedCode();
			//	resultMap.put("adrgCode", e.getParADRGCode());
				List<QuotaExpected> dtlList = quotaExpectApplication.getQuotaExpectedByCode(eCode);
				for(QuotaExpected quotaExpected: dtlList){
					if("AvgDays".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgDays", quotaExpected.getExpectedValue());
					}
					if("AvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgFee", quotaExpected.getExpectedValue());
					}
					if("HCAvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcAvgFee", quotaExpected.getExpectedValue());
					}
					if("DrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugFee", quotaExpected.getExpectedValue());
					}
					if("TotalCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("totalCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCTotalFund".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcTotalFund", quotaExpected.getExpectedValue());
					}
					if("TimeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("timeEff", quotaExpected.getExpectedValue());
					}
					if("FeeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("feeEff", quotaExpected.getExpectedValue());
					}
					if("DrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugMateFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugMateFee", quotaExpected.getExpectedValue());
					}
				}
				resultList.add(resultMap);
			*/}
		}else{
			return null;
		}
		return InvokeResult.success(resultList);
	}

	@Override
	public InvokeResult getLastResultDRG(IndexCalDTO dto) {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<Expected> expected = expectedApplication.getExpected(dto.getType1(),ForeCastFacadeImpl.year, "drg");
		if(expected.size()>0){
			for(int i=0; i<expected.size(); i++){/*
				Map<String, Object> resultMap = new HashMap<>();
				Expected e = expected.get(i);
				String eCode = e.getExpectedCode();
			//	resultMap.put("drgCode", e.getPardrgCode());
			//	resultMap.put("drgDesc", drgsApplication.getDRGsDesc(e.getPardrgCode())); 
				List<QuotaExpected> dtlList = quotaExpectApplication.getQuotaExpectedByCode(eCode);
				for(QuotaExpected quotaExpected: dtlList){
					if("AvgDays".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgDays", quotaExpected.getExpectedValue());
					}
					if("AvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("avgFee", quotaExpected.getExpectedValue());
					}
					if("HCAvgFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcAvgFee", quotaExpected.getExpectedValue());
					}
					if("DrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugFee", quotaExpected.getExpectedValue());
					}
					if("TotalCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("totalCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCCaseCnt".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcCaseCnt", quotaExpected.getExpectedValue());
					}
					if("HCTotalFund".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcTotalFund", quotaExpected.getExpectedValue());
					}
					if("TimeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("timeEff", quotaExpected.getExpectedValue());
					}
					if("FeeEff".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("feeEff", quotaExpected.getExpectedValue());
					}
					if("DrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("drugMateFee", quotaExpected.getExpectedValue());
					}
					if("HCDrugMateFee".equals(quotaExpected.getParQuotaCode())){
						resultMap.put("hcDrugMateFee", quotaExpected.getExpectedValue());
					}
				}
				resultList.add(resultMap);
			*/}
		}else{
			return null;
		}
		return InvokeResult.success(resultList);
	}
}
